class Solution {

    private Set<List<Integer>> res;
    private List<List<Integer>> ans;
    private List<Integer> per;
    private int length;
    private int[] used;

    public List<List<Integer>> permuteUnique(int[] nums) {
        ans = new ArrayList();
        per = new ArrayList();
        res = new HashSet();
        length = nums.length;
        used = new int[length];
        recur(nums, 0);
        ans.addAll(res);
        return ans;
    }

    private void recur(int[] nums, int n){
        if(n==length){
            res.add(new ArrayList(per));
            return;
        }
        //遍历每个元素，根据是否使用添加到排列中
        for(int i=0; i<length; i++){
            if(used[i]==1)continue;
            used[i]=1;
            per.add(nums[i]);
            recur(nums, n+1);
            //还原现场
            used[i]=0;
            per.remove(per.size()-1);
        }
    }
}